package com.xkstream.client.presenter;

import com.xkstream.client.api.LiveApi;
import com.xkstream.client.api.LiveService;
import com.xkstream.client.api.VersionApi;
import com.xkstream.client.api.VersionService;
import com.xkstream.client.application.Application;
import com.xkstream.client.base.BaseData;
import com.xkstream.client.base.BasePresenterImpl;
import com.xkstream.client.entity.FirVersion;
import com.xkstream.client.entity.Game;
import com.xkstream.client.entity.User;
import com.xkstream.client.entity.kt.GiftRankingData;
import com.xkstream.client.rx.RxHelper;
import com.xkstream.client.rx.RxObserver;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by xiaoyi on 2017/8/25.
 */

public class StartLivePresenter extends BasePresenterImpl implements StartLiveContract.Presenter {
    private StartLiveContract.View view;
    private LiveService liveService;
    private VersionService versionService;

    public StartLivePresenter(StartLiveContract.View view) {
        this.view = view;
        this.liveService = LiveApi.getInstance().service;
        this.versionService = VersionApi.getInstance().service;
    }

    @Override
    public void anchorOnOff(Map<String, Object> params) {
        User user = Application.getUser();
        Map<String, Object> anchorParams = new HashMap<>();
        anchorParams.put("anchor_user_code", user.getUser_code());
        anchorParams.put("currency", "CNY");
        view.showLoading();
        /*liveService.getAnchorDetail(anchorParams)
                .flatMap(new Function<BaseData<List<AnchorInfo>>, ObservableSource<BaseData<Game>>>() {
                    @Override
                    public ObservableSource<BaseData<Game>> apply(@NonNull BaseData<List<AnchorInfo>> listBaseData) throws Exception {
                        if (listBaseData.getData() != null) {
                            user.setAnchor(listBaseData.getData().get(0));
                            PreferenceUtils.putString(PreferenceUtils.USER,
                                    new Gson().toJson(user));
                        }
                        return liveService.anchorOnOff(params);
                    }
                }).compose(RxHelper.toUI()).subscribe(new RxObserver<BaseData<Game>>(this) {
            @Override
            protected void onSuccess(BaseData<Game> data) {
                view.hideLoading();
                view.showAnchorOnOffResult(data, params);
            }

            @Override
            protected void onFail(String message) {
                view.hideLoading();
            }
        });*/
        liveService.anchorOnOff(params).compose(RxHelper.toUI())
                .subscribe(new RxObserver<BaseData>(this) {
                    @Override
                    protected void onSuccess(BaseData data) {
                        view.hideLoading();
                        view.showAnchorOnOffResult(data, params);
                    }

                    @Override
                    protected void onFail(String message) {
                        view.hideLoading();
                    }
                });
    }

    @Override
    public void getGameType() {
        view.showLoading();
        liveService.getGameType().compose(RxHelper.toUI())
                .subscribe(new RxObserver<BaseData>(this) {
                    @Override
                    protected void onSuccess(BaseData baseData) {

                    }

                    @Override
                    protected void onFail(String message) {

                    }
                });
    }

    @Override
    public void getGiftRanking(String userCode) {
        view.showLoading();
        Map<String, Object> params = new HashMap<>();
        params.put("anchor_user_code", userCode);
        liveService.getGiftRanking(params)
                .compose(RxHelper.toUI())
                .subscribe(new RxObserver<BaseData<GiftRankingData>>(this) {
                    @Override
                    protected void onSuccess(BaseData<GiftRankingData> baseData) {
                        view.hideLoading();
                        view.showRankingData(baseData.getData());
                    }

                    @Override
                    protected void onFail(String message) {
                        view.hideLoading();
                    }
                });
    }

    @Override
    public void checkVersion() {
        view.showLoading();
        versionService.getLatestVersionFromFir(VersionService.FIR_APITOKEN)
                .compose(RxHelper.toUI())
                .subscribe(new RxObserver<FirVersion>(this) {
                    @Override
                    protected void onSuccess(FirVersion firVersion) {
                        view.hideLoading();
                        view.showVersionInfo(firVersion);
                    }

                    @Override
                    protected void onFail(String message) {
                        view.hideLoading();
                    }
                });
    }
}
